/**
 * 自定义配置
 * */

var domain = 'http://api.jiuge_freecode.com';
var adminLogin; //登录信息
var routes = {
    login: '/login.html',   //登录路由
    index: '/index.html',   //首页
    scenceIndex: '/views/scence.html',  //场景
    videoIndex: '/views/video.html',  //视频
    qrIndex: '/views/qr.html',  //二维码
    qrLogIndex: '/views/qrlog.html'  //扫码
}; //前端页面路由地址
var apis = {
    login: {    //登录
        method: 'post',
        url: '/admin/login'
    },
    scenceIndex: {  //场景列表
        method: 'get',
        url: '/admin/scence/index'
    },
    videoIndex: {   //视频列表
        method: 'get',
        url: '/admin/video/index'
    },
    qrIndex: {   //码列表
        method: 'get',
        url: '/admin/qr/index'
    },
    qrLogIndex: {   //扫码列表
        method: 'get',
        url: '/admin/qrlog/index'
    }
}; //后端接口地址


//抓图
function uploadImage() {
    var img = $("#viewimg"), dataURL, fileObj = $('#uploadFile')[0],
        windowURL = window.URL || window.webkitURL, reader = new FileReader();
    dataURL = windowURL.createObjectURL(fileObj.files[0]); //要显示图片的路径
    img.attr("src", dataURL); //显示图片
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (e){
        //console.log(e.target.result);
        //$('#img')[0].value = e.target.result; //这一步是关键
        showImg(e.target.result);
    }
}


//登录aid
function getAid(){
    var adminInfo = getAdminInfo();
    if (!adminInfo) {
        msgPop({msg: '没有登录'}); return
    }
    return adminInfo.aid + '<>' + adminInfo.adminName;
}


// 分离header头部
function getHeader(){
    // <div class="navbar navbar-fixed-top"></div>
    //     <div class="container-fluid cl" style="background: #222;"></div>
    //         <a class="logo navbar-logo" href="javascript:;" style="color: #fff;">管理后台</a>
    //         <nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs"></nav>
    //             <ul class="cl">
    //                 <li class="dropDown dropDown_hover">
    //                     <a href="javascript:;" class="dropDown_A" style="color: #fff;"></a>
    //                         admin <i class="Hui-iconfont">&#xe6d5;</i>
    //                     </a>
    //                     <ul class="dropDown-menu menu radius box-shadow">
    //                         <li><a href="#">退出</a></li>
    //                     </ul>
    //                 </li>
    //             </ul>
    //         </nav>
    //     </div>
    // </div>
    var h = '';
    h += '<div class="navbar navbar-fixed-top">';
    h += '<div class="container-fluid cl" style="background: #222;">';
    h += '<a class="logo navbar-logo" href="javascript:;" style="color: #fff;">管理后台</a>';
    h += '<nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs">';
    h += '<ul class="cl">';
    h += '<li class="dropDown dropDown_hover">';
    h += '<a href="javascript:;" class="dropDown_A" style="color: #fff;">';
    h += 'admin';
    h += ' <i class="Hui-iconfont">&#xe6d5;</i>';
    h += '</a>';
    h += '<ul class="dropDown-menu menu radius box-shadow">';
    h += '<li><a href="#">退出</a></li>';
    h += '</ul>';
    h += '</li>';
    h += '</ul>';
    h += '</nav>';
    h += '</div>';
    h += '</div>';
    $('#navHeader').html(h);
}


// 分离左侧menu
function getLeftMenu(){
    // <div class="menu_dropdown bk_2">
    //     <dl id="menu-article">
    //         <dt>
    //             <i class="Hui-iconfont">&#xe616;</i> 
    //             资讯管理
    //             <i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>
    //         </dt>
    //         <dd>
    //             <ul>
    //                 <li>
    //                     <a href="article-list.html" title="资讯管理">资讯管理</a>
    //                 </li>
    //             </ul>
    //         </dd>
    //     </dl>
    //     <dl id="menu-member">
    //         <dt>
    //             <i class="Hui-iconfont">&#xe60d;</i> 
    //             会员管理
    //             <i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>
    //         </dt>
    //         <dd>
    //             <ul>
    //                 <li>
    //                     <a href="member-list.html" title="会员列表">会员列表</a>
    //                 </li>
    //             </ul>
    //         </dd>
    //     </dl>
    // </div>
    var ms = [
        {
            id: 1,
            title: '码源管理',
            childs: [
                { id: 11, title: '场景', link: 'scence.html' },
                //{ id: 12, title: '视频', link: '' },
                { id: 13, title: '二维码', link: 'qr.html' },
                { id: 14, title: '扫码统计', link: 'qrlog.html' }
            ]
        },
        {
            id: 2,
            title: '用户管理',
            childs: [
                { id: 21, title: 'H5用户', link: '' },
                { id: 22, title: 'APP用户', link: '' },
                { id: 23, title: 'EXE用户', link: '' },
                { id: 24, title: '个人用户', link: '' },
                { id: 25, title: '商家用户', link: '' }
                //{ title: '企业用户', link: '' }
            ]
        },
        {
            id: 3,
            title: '业务管理',
            childs: [
                { id: 31, title: '模板1', link: '' },
                { id: 32, title: '模板1评论', link: '' },
                { id: 33, title: '模板1历史', link: '' },
                { id: 34, title: '模板1关联', link: '' }
            ]
        },
        {
            id: 4,
            title: '广告管理',
            childs: [
                { id: 41, title: 'H5广告位', link: '' },
                { id: 42, title: '商家添加', link: '' },
                { id: 43, title: '排队中', link: '' },
                { id: 44, title: '已展示', link: '' }
                //{ title: '产品推荐', link: '' }
            ]
        },
        {
            id: 5,
            title: '收支管理',
            childs: [
                { id: 51, title: '平台收入', link: '' },
                { id: 52, title: '商家收入', link: '' },
                { id: 53, title: '广告付费', link: '' }
            ]
        }
    ];
    var h = '', i, ii, childs;
    h += '<div class="menu_dropdown bk_2">';
    for (i = 0; i < ms.length; i ++) {
        h += '<dl id="menu-article">';
        //h += '<dt>';
        h += '<dt class="selected">';
        h += '<i class="Hui-iconfont">&#xe616;</i> ';
        h += ms[i].title;
        h += '<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>';
        h += '</dt>';
        //h += '<dd>';
        h += '<dd style="display: block;">';
        h += '<ul>';
        childs = ms[i].childs;
        for (ii = 0; ii < childs.length; ii ++) {
            h += '<li>';
            h += '<a href="javascript:;" onclick="toLeftMenu(' + ii + ');">';
            h += childs[ii].title;
            h += '<span style="margin-right: 10px; float: right;" class="lma" id="lma-' + ii + '"></span>';
            h += '</a>';
            h += '<input type="hidden" id="mlink-' + ii + '" value="/views/' + childs[ii].link + '"/>';
            h += '</li>';
        }
        h += '</ul>';
        h += '</dd>';
        h += '</dl>';
    }
    h += '</div>';
    $('#leftMenu').html(h);
}


//切换菜单
function toLeftMenu(v){
    var link = $('#mlink-' + v).val();
    if (link == '') {
        msgPop({msg: '没有地址'}); return
    }
    $('.lma').html('');
    $('#lma-' + v).html('→');
    $('iframe').attr('src', link);
}
//切换首页
function toHome(){
    $('.lma').html('');
    $('iframe').attr('src', '/index.html');
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//获取登录信息
function getAdminInfo(){
    return getCache(adminLogin);
}


//设置登录信息
function setAdminInfo(e){
    return setCache({key: adminLogin, val: e.val});
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}